package com.archive.manage.service.impl;

import com.alibaba.fastjson2.JSON;
import com.archive.manage.Utils;
import com.archive.manage.domain.StuArchive;
import com.archive.manage.domain.entity.*;
import com.archive.manage.http.ApacheHttpUtils;
import com.archive.manage.service.IBlockchainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

public class BlockchainServiceImpl implements IBlockchainService {
    @Autowired
    private RestTemplate restTemplate;

    private static final String BASE_URL = "http://192.168.197.40:18899/v1/chaincodes/invoke/archive/";

    @Override
    public ArchiveCreateResponse createArchive(String ccname, String channel, String caller, String operator, StuArchive archive) throws Exception {
        String url = BASE_URL + "create";
        return createOrUpdate(ccname, channel, caller, operator, archive, url);
    }

    @Override
    public boolean existArchive(String ccname, String channel, String caller, String id) throws Exception {
        String url = BASE_URL + "exist";
        ArchiveRequestEntity requestEntity = Utils.generateBlockChainRequestEntity(ccname, channel, caller, id);
        ArchiveExistResponse response = ApacheHttpUtils.doGetOrDeleteWithRequestBody(url, JSON.toJSONString(requestEntity), "GET", ArchiveExistResponse.class);
        if (response.getCode() != 1) {
            throw new Exception("Invoke exist API failed!");
        }
        return response.isResult();
    }

    @Override
    public ArchiveCreateResponse update(String ccname, String channel, String caller, String operator, StuArchive newArchive) throws Exception {
        String url = BASE_URL + "update";
        return createOrUpdate(ccname, channel, caller, operator, newArchive, url);
    }

    private ArchiveCreateResponse createOrUpdate(String ccname, String channel, String caller, String operator, StuArchive newArchive, String url) throws Exception {
        ArchiveRequestEntity requestEntity = Utils.generateBlockChainRequestEntity(ccname, channel, caller, operator, newArchive);
        ArchiveCreateResponse response = ApacheHttpUtils.doPost(url, JSON.toJSONString(requestEntity), ArchiveCreateResponse.class);
        if (response.getCode() != 1) {
            throw new Exception("Invoke create or update API failed!");
        }
        return response;
    }

    @Override
    public ArchiveGetResult getArchive(String ccname, String channel, String caller, String id) throws Exception {
        String url = BASE_URL + "get";
        ArchiveRequestEntity requestEntity = Utils.generateBlockChainRequestEntity(ccname, channel, caller, id);
        ArchiveGetResponse response = ApacheHttpUtils.doGetOrDeleteWithRequestBody(url, JSON.toJSONString(requestEntity), "GET", ArchiveGetResponse.class);
        if (response.getCode() != 1) {
            throw new Exception("Invoke exist API failed!");
        }
        String resultStr = response.getResult().replace("\\", "").replace("\"{", "{").replace("}\"", "}");
        return JSON.parseObject(resultStr, ArchiveGetResult.class);
    }

    @Override
    public ArchiveGetResponse deleteArchive(String ccname, String channel, String caller, String id) throws Exception {
        String url = BASE_URL + "delete";
        ArchiveRequestEntity requestEntity = Utils.generateBlockChainRequestEntity(ccname, channel, caller, id);
        ArchiveGetResponse response = ApacheHttpUtils.doGetOrDeleteWithRequestBody(url, JSON.toJSONString(requestEntity), "DELETE", ArchiveGetResponse.class);
        if (response.getCode() != 1) {
            throw new Exception("Invoke exist API failed!");
        }
        //String resultStr = response.getResult().replace("\\", "").replace("\"{", "{").replace("}\"", "}");
        return response;
    }
}
